package org.csu.mypetstore.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Inventory;
import org.csu.mypetstore.domin.Item;
import org.csu.mypetstore.domin.Product;
import org.csu.mypetstore.persistence.InventoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.csu.mypetstore.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service("ItemService")
public class ItemServiceImpl implements ItemService  {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private ProductServiceImpl productService;

    //更新item库存数量
    @Override
    public CommonResponse<Inventory> updateInventoryQuantity(String itemid, int num) {

        QueryWrapper<Inventory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("itemid",itemid);
        Inventory inventory=inventoryMapper.selectOne(queryWrapper);
        int x=inventory.getQty();
        if(x<num){
            return CommonResponse.createForError("没有这么多数量的商品");
        }else{
            x-=num;
        }
        inventory.setQty(x);
        UpdateWrapper<Inventory> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("itemid",itemid);
        int y=inventoryMapper.update(inventory,updateWrapper);
        if(y==0){
            return CommonResponse.createForError("服务器内部异常");
        }
        return CommonResponse.createForSuccess("更新商品数量成功",inventory);
    }

    //获得item库存数量
    @Override
    public CommonResponse<Inventory> getInventoryQuantity(String itemId) {
        QueryWrapper<Inventory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("itemid",itemId);

        Inventory inventory=inventoryMapper.selectOne(queryWrapper);
        if(itemId==null){
            return CommonResponse.createForError("通过itemId无法查找商品数量");
        }
        return CommonResponse.createForSuccess("查找成功",inventory);
    }

    //通过productid获得item列表
    @Override
    public CommonResponse<JSONObject> getItemListByProduct(String productId) {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productid",productId);

        List<Item> itemList=itemMapper.selectList(queryWrapper);
        if(itemList==null){
            return CommonResponse.createForError("通过productId无法查找商品信息");
        }
        Product product = productService.getProduct(productId).getData();
        for(Item item : itemList){
            Inventory inventory = inventoryMapper.selectOne(new QueryWrapper<Inventory>().eq("itemid",item.getItemid()));
            item.setQty(inventory.getQty());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productInfo",product);
        jsonObject.put("items",itemList);
        return CommonResponse.createForSuccess("查找成功",jsonObject);
    }


    //获得item
    @Override
    public CommonResponse<Item> getItem(String itemId) {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("itemid",itemId);

         Item item=itemMapper.selectOne(queryWrapper);
        if(itemId==null){
            return CommonResponse.createForError("通过itemId无法查找商品信息");
        }
        return CommonResponse.createForSuccess("查找成功",item);
    }

    @Override
    public  CommonResponse<Item> updateItem(Item item){
        UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("itemid",item.getItemid());
        int y=itemMapper.update(item,updateWrapper);
        if(y==0){
            return CommonResponse.createForError("服务器内部异常");
        }
        return CommonResponse.createForSuccess("更新item成功",item);
    }
    @Override
    public  CommonResponse<List<Item>> fourHotItems(){
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("click").last("limit 4");
        List<Item> itemList=itemMapper.selectList(queryWrapper);
        for(Item item : itemList){
            item.setProduct(productService.getProduct(item.getProductid()).getData());

        }
        return CommonResponse.createForSuccess("获得最火的四个item",itemList);
    }
}
